package com.demo.crm.service;

import com.arvato.crm.model.FilterRule;
import com.arvato.crm.model.PagingInfo;
import com.arvato.crm.repository.NamedQueryTools;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Service;

import javax.persistence.Query;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author  fzd
 */
@Service
public class MemberService {
    /**
     * @author fzd
     * 会员信息360，基本信息
     * @param memberId
     * @return
     */
    public Map<String,Object> member360InfoBasic(final String memberId){
        List<Map<String,Object>> list=NamedQueryTools.queryForMap(
            NamedQueryTools.createDynamicSqlQuery("member360InfoBasic", null).setParameter("memberId",memberId)
        );

        return list==null?null:list.get(0);
    }

    /**
     * 会员消费统计,半年内每月的消费情况
     * @param memberId
     * @return
     */
    public List<Map<String,Object>> memberExpenseStatistics(final String memberId){
        return NamedQueryTools.queryForMap(
                NamedQueryTools.createDynamicSqlQuery("memberExpenseStatistics", null)
                    .setParameter("memberId",memberId)
                    .setParameter("memberId2",memberId)
        );
    }


    /**
     * 会员统计
     * @param param
     * @return
     */
    public List<Map<String,Object>> memberStatistics(Map<String,Object> param){
        Query query=NamedQueryTools.createDynamicSqlQuery("memberStatistics",param);
        if(param!=null){
            for(String fieldName:param.keySet()){
                query.setParameter(fieldName,param.get(fieldName));
            }
        }

        return NamedQueryTools.queryForMap(query);
    }

    /**
     * 会员统计，每月的总计
     * @param param
     * @return
     */
    public List<Map<String,Object>> memberStatisticsMonthTotal(Map<String,Object> param){
        Query query=NamedQueryTools.createDynamicSqlQuery("memberStatisticsMonthTotal",param);
        if(param!=null){
            for(String fieldName:param.keySet()){
                query.setParameter(fieldName,param.get(fieldName));
            }
        }

        return NamedQueryTools.queryForMap(query);
    }

    /**
     * 会员统计，每种顾客的新增数
     * @param param
     * @return
     */
    public Map<String,Object> memberStatisticsCustomerTypeTotal(Map<String,Object> param){
        Query query=NamedQueryTools.createDynamicSqlQuery("memberStatisticsCustomerTypeTotal",param);
        if(param!=null){
            for(String fieldName:param.keySet()){
                query.setParameter(fieldName,param.get(fieldName));
            }
        }

        return NamedQueryTools.queryForMap(query).get(0);
    }

    /**
     * 积分分布  有效积分分布
     * @param param
     * @return
     */
    public List<Map<String,Object>> pointStatistics(Map<String,Object> param){
        Query query=NamedQueryTools.createDynamicSqlQuery("pointStatistics",param);
        if(param!=null){
            for(String fieldName:param.keySet()){
                query.setParameter(fieldName,param.get(fieldName));
            }
        }

        return NamedQueryTools.queryForMap(query);
    }

    /**
     * 积分分布  会员积分增加情况
     * @param param
     * @return
     */
    public List<Map<String,Object>> memberPointStatistics(Map<String,Object> param){
        Query query=NamedQueryTools.createDynamicSqlQuery("memberPointStatistics",param);
        if(param!=null){
            for(String fieldName:param.keySet()){
                query.setParameter(fieldName,param.get(fieldName));
            }
        }

        return NamedQueryTools.queryForMap(query);
    }
}
